package com.peycard.persistence.dao;

import java.math.BigDecimal;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.peycard.persistence.PersistenceException;
import com.peycard.persistence.dao.local.FinancialTransactionDAOLocal;
import com.peycard.persistence.entities.FinancialTransaction;

/**
 * Session Bean implementation class FinancialTransactionDAO
 */
@Stateless
public class FinancialTransactionDAO extends GenericDAOImpl<FinancialTransaction, Long> implements FinancialTransactionDAOLocal {

    /**
     * Default constructor. 
     */
    public FinancialTransactionDAO() {
    }
    
    @Override
    public List<FinancialTransaction> findByAutNo(String autNo) throws PersistenceException{
    	try{
    		BigDecimal bd = new BigDecimal(autNo);
    		Query query = getEntityManager().createNamedQuery("findTransactionsByAut_1");
    		query.setParameter("aut_1", bd);
    		List<FinancialTransaction> list = query.getResultList();
			return list;
    	}catch(Exception e){
    		throw new PersistenceException(e);
    	}
    }
    
    @Override
    public List<FinancialTransaction> findByAmount(String amount) throws PersistenceException{
    	try{
    		BigDecimal bd = new BigDecimal(amount);
    		Query query = getEntityManager().createNamedQuery("findTransactionsByAmount_1");
    		query.setParameter("amount_1", bd);
    		List<FinancialTransaction> list = query.getResultList();
			return list;
    	}catch(Exception e){
    		throw new PersistenceException(e);
    	}
    }
    
    @Override
    public List<FinancialTransaction> findByStatus(String status) throws PersistenceException{
    	try{
    		Query query = getEntityManager().createNamedQuery("findTransatcionByStatus_1");
    		query.setParameter("status_1", status);
    		List<FinancialTransaction> list = query.getResultList();
			return list;
    	}catch(Exception e){
    		throw new PersistenceException(e);
    	}
    }
    
    @Override
    public List<FinancialTransaction> findByDates(String beginDate, String endDate) throws PersistenceException{
    	try{
    		Query query = getEntityManager().createNamedQuery("findTransactionByDateNative_1");
    		query.setParameter(1, beginDate);
    		query.setParameter(2, endDate);
    		List<FinancialTransaction> list = query.getResultList();
    		return list;
    	}catch(Exception e){
    		throw new PersistenceException(e);
    	}
    }
    
    @Override
    public List<FinancialTransaction> findByPan(String pan) throws PersistenceException{
    	try{
    		Query query = getEntityManager().createNamedQuery("findTransactionByPan_1");
			query.setParameter("pan_1", "%" + pan);
			List<FinancialTransaction> list = query.getResultList();
			return list;
    	}catch(Exception e){
    		throw new PersistenceException(e);
    	}
    }

}
